// ==UserScript==
// @name           Vkontakte Calculate Age
// @namespace      http://polkila.googlecode.com
// @author         Васютинский Олег http://vasyutinskiy.ru
// @version        10.12
// @include        http://*vkontakte.ru/*
// @include        http://*vk.com/*
// ==/UserScript==

var scriptName = 'VkontakteRu.CalculateAge';
var scriptVersion = 10.12;
var lang = parseInt(readCookie('remixlang'));

(function(){
	var bdate = null, byear = null, _age = '', _sign = '', now = new Date(), as = document.evaluate('//div[@class="profile_info"]//a', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

	for (var a = null, i = 0; (a = as.snapshotItem(i)); i++){
		bdate = /c[\[%5B]{1,3}bday[\]%5D]{1,3}=([0-9]{1,2})[&amp;]{1,5}c[\[%5B]{1,3}bmonth[\]%5D]{1,3}=([0-9]{1,2})/.exec(a.href);
		if (bdate) break;
	}
	if (bdate){
		var t = a.parentNode;
		byear = /c[\[%5B]{1,3}byear[\]%5D]{1,3}=([0-9]{4})/.exec(t.innerHTML);
		if (lang) var signs = new Array('Capricorn','Aquarius','Pisces','Aries','Taurus','Gemini','Cancer','Leo','Virgo','Libra','Scorpio','Sagittarius');
		else var signs = new Array('&#1050;&#1086;&#1079;&#1077;&#1088;&#1086;&#1075;','&#1042;&#1086;&#1076;&#1086;&#1083;&#1077;&#1081;','&#1056;&#1099;&#1073;&#1099;','&#1054;&#1074;&#1077;&#1085;','&#1058;&#1077;&#1083;&#1077;&#1094;','&#1041;&#1083;&#1080;&#1079;&#1085;&#1077;&#1094;&#1099;','&#1056;&#1072;&#1082;','&#1051;&#1077;&#1074;','&#1044;&#1077;&#1074;&#1072;','&#1042;&#1077;&#1089;&#1099;','&#1057;&#1082;&#1086;&#1088;&#1087;&#1080;&#1086;&#1085;','&#1057;&#1090;&#1088;&#1077;&#1083;&#1077;&#1094;');
		var lastD = new Array(19,18,20,19,20,21,22,22,22,22,21,21);
		var signN = bdate[2]-1;
		if (bdate[1]>lastD[signN]) signN = (signN+1) % 12;
		_sign = signs[signN];
	}
	if (byear){
		_age = now.getFullYear() - byear[1];
		if (bdate && bdate[2]>now.getMonth()+1) _age--;
		else if (bdate && bdate[2]==now.getMonth()+1 && bdate[1]>now.getDate()) _age--;
		if (lang) _age+= ' years old, ';
		else{
			if (_age>4 && _age<21) _age+= ' &#1083;&#1077;&#1090;, ';
			else{
				last = _age.toString().substr(1);
				if (last==1) _age+= ' &#1075;&#1086;&#1076;, ';
				if (last>1 && last<5) _age+= ' &#1075;&#1086;&#1076;&#1072;, ';
				if (last>4 || last==0) _age+= ' &#1083;&#1077;&#1090;, ';
			}
		}
	}
	if (_age || _sign) t.innerHTML+= ' ('+_age+''+_sign+')';

	setTimeout(checkForUpdate, 2000);
})();

function readCookie(name){
	var nameEQ = name + '=';
	var ca = document.cookie.split(';');
	for (i=0; i<ca.length; i++){
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1, c.length);
		if (c.indexOf(nameEQ)==0) return c.substring(nameEQ.length, c.length);
	}
	return null;
}

function createCookie(name,value,days){
	if (days){
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = '; expires='+date.toGMTString();
	}
	else expires = '';
	document.cookie = name+'='+value+expires+'; path=/';
}

function checkForUpdate(){
	if (readCookie(scriptName+'.checked')) return;
	GM_xmlhttpRequest({
		method: 'GET',
		url: 'http://polkila.googlecode.com/svn/trunk/'+scriptName+'.ver',
		onload: function(result){
			createCookie(scriptName+'.checked', true, 3);
			newVer = parseFloat(result.responseText);
			if (newVer>scriptVersion && confirm(lang?'There is a new version of '+scriptName+' script.\nDo You want to update?':'Появилась новая версия скрипта '+scriptName+'.\nОбновить?')){
				location.href = 'http://polkila.googlecode.com/files/'+scriptName+'.'+newVer+'.user.js';
			}
		}
	});
}
